package com.shanghaionstar.weight.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.shanghaionstar.R;

/**
 * 普通对话框:标题、内容、一个按钮
 * Created by Longmin Li on 2017/7/13.
 */

public class GeneralDialog extends Dialog implements View.OnClickListener {
    //按钮
    private Button submitBt;
    //消息标题文本
    private TextView titleTv;
    //消息提示文本
    private TextView messageTv;
    //从外界设置的title文本
    private String titleStr;
    //从外界设置的消息文本
    private String messageStr;
    //按钮文字
    private String buttonStr;
    //按钮点击监听器
    private OnClickListener onclickListener;

    public GeneralDialog(@NonNull Activity activity) {
        super(activity, R.style.general_dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_general_layout);
        initView();
        initData();

    }

    private void initView() {
        //按空白处不能取消
        setCanceledOnTouchOutside(false);

        titleTv = (TextView) findViewById(R.id.title_tv);
        messageTv = (TextView) findViewById(R.id.message_tv);
        submitBt = (Button) findViewById(R.id.submit_bt);
        submitBt.setOnClickListener(this);

    }

    private void initData() {
        //如果自定了title和message
        if (titleStr != null) {
            titleTv.setText(titleStr);
        }
        if (messageStr != null) {
            messageTv.setText(messageStr);
        }
        //如果设置按钮的文字
        if (buttonStr != null) {
            submitBt.setText(buttonStr);
        }
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.submit_bt) {
            onclickListener.onClick();
        }
    }


    /**
     * 设置dialog标题
     *
     * @param title
     */
    public GeneralDialog setTitle(String title) {
        titleStr = title;
        return this;
    }

    /**
     * 设置dialog的message
     *
     * @param message
     */
    public GeneralDialog setMessage(String message) {
        messageStr = message;
        return this;
    }

    /**
     * 设置按钮文字和监听
     *
     * @param buttonStr
     * @param onClickListener
     */
    public void setOnSubmitOnclickListener(String buttonStr, OnClickListener onClickListener) {
        if (buttonStr != null) {
            this.buttonStr = buttonStr;
        }
        this.onclickListener = onClickListener;
    }


    public interface OnClickListener {
        public void onClick();
    }


}
